/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.diplomka.servicelayer.gamelogic.managerimpl;

import cz.fi.muni.diplomka.common.enumeration.UnitType;
import cz.muni.fi.diplomka.datalayer.dao.CityUnitsDao;
import cz.muni.fi.diplomka.datalayer.daoimpl.CityUnitsDaoImpl;
import cz.muni.fi.diplomka.datalayer.entity.CityUnitsEntity;
import cz.muni.fi.diplomka.servicelayer.dto.CityUnits;
import cz.muni.fi.diplomka.servicelayer.gamelogic.manager.CityUnitsManager;
import cz.muni.fi.diplomka.servicelayer.xmlconvertors.LimitsConvertor;
import java.util.Map;
import org.dozer.DozerBeanMapper;
import org.dozer.Mapper;

/**
 *
 * @author Jakub Kutil
 */
public class CityUnitsManagerImpl implements CityUnitsManager {

    private CityUnitsDao unitsInCityDao = new CityUnitsDaoImpl();
    private Mapper mapper = new DozerBeanMapper();

    @Override
    public void lowerUnitCount(Long cityId, UnitType unitType, int count) {
        if (cityId == null) {
            throw new IllegalArgumentException("CityId can not be null.");
        }
        if (unitType == null) {
            throw new IllegalArgumentException("UnitType can not be null.");
        }

        if (count <= 0) {
            throw new IllegalArgumentException("Count can not be lower than zero.");
        }
        CityUnits cityUnits = new CityUnits();
        cityUnits.setCityId(cityId);
        cityUnits.setUnit(unitType);
        cityUnits.setUnitCount(this.getUnitCount(cityId, unitType) - count);

        unitsInCityDao.editUnitsInCity(mapper.map(cityUnits, CityUnitsEntity.class));
    }

    @Override
    public void increaseUnitCount(Long cityId, UnitType unitType, int count) {
        if (cityId == null) {
            throw new IllegalArgumentException("CityId can not be null.");
        }
        if (unitType == null) {
            throw new IllegalArgumentException("UnitType can not be null.");
        }

        if (count <= 0) {
            throw new IllegalArgumentException("Count can not be lower than zero.");
        }
        CityUnits cityUnits = new CityUnits();
        cityUnits.setCityId(cityId);
        cityUnits.setUnit(unitType);
        cityUnits.setUnitCount(this.getUnitCount(cityId, unitType) + count);

        unitsInCityDao.editUnitsInCity(mapper.map(cityUnits, CityUnitsEntity.class));
    }

    @Override
    public int getUnitCount(Long cityId, UnitType unitType) {
        if (cityId == null) {
            throw new IllegalArgumentException("CityId can not be null.");
        }
        if (unitType == null) {
            throw new IllegalArgumentException("UnitType can not be null.");
        }

        return unitsInCityDao.getSpecificUnitCountInCity(cityId, unitType);
    }

    @Override
    public void createStartingUnitsForCity(Long cityId) {
        LimitsConvertor limitsConvertor = new LimitsConvertor();

        if (cityId == null) {
            throw new IllegalArgumentException("CityId cant be null");
        }
        Map<String, Integer> limits = limitsConvertor.getStartingUnitsFromXML();

        for (UnitType unit : UnitType.values()) {
            CityUnits cityUnits = new CityUnits();
            cityUnits.setCityId(cityId);
            cityUnits.setUnit(unit);

            cityUnits.setUnitCount(limits.get(unit.toString()));
            unitsInCityDao.addUnitsInCity(mapper.map(cityUnits, CityUnitsEntity.class));
        }
    }
}
